package leetcode;

/**
 * @program: javase
 * @description:
 * @author: hmx
 * @create: 2021-10-08 21:15
 **/
public class LeetCode704 {
    /*public int search(int[] nums, int target) {
        return search(0, nums.length - 1, nums, target);
    }

    public int search(int low, int high, int[] nums, int target) {
        if (low <= high) {
            int avg = (low + high) >> 1;
            if (nums[avg] == target) {
                return avg;
            } else if (nums[avg] < target) {
                return search(avg + 1, high, nums, target);
            }
            return search(low, avg - 1, nums, target);
        }
        return -1;

    }*/

    public int search(int[] nums, int target) {
        int low = 0;
        int high = nums.length;
        while (low <= high) {
            int mid = ((high - low) >> 1) + low;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        LeetCode704 code = new LeetCode704();
        int[] nums = {-1,0,3,5,9,12};
        System.out.println(code.search(nums, 9));
    }


}
